<?php
if (!function_exists('http_build_query')) {
    function http_build_query($arr,$num_prefix='',$top_k='') {
        $result='';
        foreach ((array)$arr as $k => $v) {
            $k=urlencode(is_int($k)?$num_prefix.$k:$k);
            if ($top_k) {
                $tmp_key=$top_k.'%5B'.$k.'%5D';
            } else {
                $tmp_key=urlencode($k);
            }
            if (is_array($v) || is_object($v)) {
                $result.='&'.http_build_query($v,'',$tmp_key);
            } elseif (is_scalar($v)) {
                $result.='&'.$tmp_key.'='.urlencode($v);
            }
        }
        return substr($result,1);
    }
}
function httpRawPost($url,$post_str) {
    $urls=parse_url($url);
    if (!isset($url['host'])) {
        return false;
    }
    $host=$urls['host'];
    $path=isset($urls['path'])?$urls['path']:'/';
    if (isset($urls['query'])) {
        $path.='?'.$urls['query'];
    }
    $port=isset($urls['port'])?$urls['port']:80;
    $fp=fsockopen($host,$port,$errno,$errstr,30);
    if (!$fp) {
        return false;
    }
    $request ='POST '.$path." HTTP/1.1\r\n";
    $request.='Host: '.$host."\r\n";
    $request.="User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; zh-CN; rv:1.9.0.6) Gecko/2009011912 Firefox/3.0.6\r\n";
    $request.="Referer: http://".$host."/\r\n";
    $request.="Content-type: application/x-www-form-urlencoded\r\n";
    $request.="Cookie: PHPSESS=zae;name=surfchen\r\n";
    $request.="Accept: */*\r\n";
    $request.="Connection: Close\r\n";
    $request.='Content-Length: '.strlen($post_str)."\r\n";
    $request.="\r\n";
    $request.=$post_str;
    fwrite($fp,$request);
    $response='';
    while (!feof($fp)) {
        $response.=fgets($fp,512);
    }
    return $response;

}
function httpPost($url,$post_data) {
    return httpRawPost($url,http_build_query($post_data));
}
function httpGet($url) {
    $urls=parse_url($url);
    if (!isset($url['host'])) {
        return false;
    }
    $host=$urls['host'];
    $path=isset($urls['path'])?$urls['path']:'/';
    if (isset($urls['query'])) {
        $path.='?'.$urls['query'];
    }
    $port=isset($urls['port'])?$urls['port']:80;
    $fp=fsockopen($host,$port,$errno,$errstr,30);
    if (!$fp) {
        return false;
    }
    $request ='GET '.$path." HTTP/1.1\r\n";
    $request.='Host: '.$host."\r\n";
    $request.="Connection: Close\r\n";
    $request.="\r\n";
    fwrite($fp,$request);
    echo $request;
    $response='';
    while (!feof($fp)) {
        $response.=fgets($fp,512);
    }
    $response=substr($response,strpos($response,"\r\n\r\n")+4);
    return $response;
}

function httpGet2($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    $headers = array(
            "Accept-Language: en-us,en;q=0.5",
            "Accept-Charset: gbk,utf-8;q=0.7,*;q=0.7",
            "User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; zh-CN; rv:1.9.0.6) Gecko/2009011912 Firefox/3.0.6",
            "Connection: Close"
            );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    return curl_exec($ch);
}
function parseHttpResponse() {
}


/*
    stream context way
    http://www.php.net/manual/en/context.http.php
*/

//$context  = stream_context_create($opts);
//echo file_get_contents($url, false, $context);
$httpreq=new httpreq;
$httpreq->setUrl('http://wordpress.sc');
$httpreq->actAs(httpreq::AGENT_FIREFOX_3_5_MAC);
$res=$httpreq->send();
var_dump($res->charset());
